import { defineStore } from 'pinia';
import { mergeDeepRight } from 'ramda';
import type { VmoxRouteLocationNamedRaw } from '@src/router/use.lib';
import store from "@src/stores/browserStorage"; // 本地持久缓存器

export declare namespace USER{
  export type State = {
    token:Record<string,string>,
    info:Record<string,any>,
    menu:VmoxRouteLocationNamedRaw[],
  }
};
export const useUserBaseStateStore = defineStore('UserState', {
  state:():USER.State=>({
    token:store.$data.userToken,
    info:store.$data.userInfo,
    menu:store.$data.userMenu,
  }),
  getters:{
    getToken:(state)=>state.token,
    getInfo:(state)=>state.info,
    getData:(state)=>mergeDeepRight(state.info,state.token),
    getMenu:(state)=>state.menu,
  },
  actions:{
    logout(){
      this.token = store.$data.userToken = {};
      this.info = store.$data.userInfo = {};
      this.menu = store.$data.userMenu = [];
    },
    setToken(token:{[key:string]:any}){
      this.token = store.$data.userToken = mergeDeepRight(store.$data.userToken,token);
    },
    setInfo(info:{[key:string]:any}){
      this.info = store.$data.userInfo = mergeDeepRight(store.$data.userToken,info);
    },
    setMenu(menu:VmoxRouteLocationNamedRaw[]){
      this.menu = store.$data.userMenu = menu;
    }
  }
});

